﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Models;
using System.Threading.Tasks;
using System.Linq;
using System.Security.Claims;
using IRepository;
using System;
using Helper;
using Microsoft.AspNetCore.Cors;
using Models.Entity;

namespace course.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [EnableCors]
    [Authorize]
    public class PublicController : ControllerBase
    {
        DapperHelper<CClassesEntity> dbHelper = new DapperHelper<CClassesEntity>();
        DapperHelper<CPlaceEntity> dbHelper_place = new DapperHelper<CPlaceEntity>();
        DapperHelper<CCourseEntity> dbHelper_course = new DapperHelper<CCourseEntity>();
        DapperHelper<CWeekCourseEntityView> dbHelper_weekCourseV = new DapperHelper<CWeekCourseEntityView>();
        DapperHelper<CTeacherCourseEntityView> dbHelper_TeacherCourse = new DapperHelper<CTeacherCourseEntityView>();
        /// <summary>
        /// 
        /// </summary>
        public PublicController()
        {

        }
        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultObj<IdName>> ClassesList()
        {
            ResultObj<IdName> reObj = new ResultObj<IdName>();
            try
            {
                reObj.success = true;
                reObj.dataList = (await dbHelper.FindAll(x => x.classes_id != 0)).Select(x => new IdName { id = x.classes_id.ToString(), name = x.name }).OrderBy(x=>x.name).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(typeof(PublicController), ex.ToString());
                reObj.msg = ex.Message;
                reObj.success = false;
            }
            return reObj;
        }

        /// <summary>
        /// 获取教师列表
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultObj<IdName>> TeacherList()
        {
            ResultObj<IdName> reObj = new ResultObj<IdName>();
            try
            {
                reObj.success = true;
                reObj.dataList = (await dbHelper_TeacherCourse.FindAll(x => x.teacher_id != 0)).Select(x => new IdName { id = x.teacher_id.ToString(), name = $"{x.teacher_name}:{x.course_name}" }).OrderBy(x=>x.name).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(typeof(PublicController), ex.ToString());
                reObj.msg = ex.Message;
                reObj.success = false;
            }
            return reObj;
        }

        /// <summary>
        /// 获取场地列表
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultObj<IdName>> PlaceList()
        {
            ResultObj<IdName> reObj = new ResultObj<IdName>();
            try
            {
                reObj.success = true;
                reObj.dataList = (await dbHelper_place.FindAll(x => x.place_id != 0)).Select(x => new IdName { id = x.place_id.ToString(), name = x.name }).OrderBy(x=>x.name).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(typeof(PublicController), ex.ToString());
                reObj.msg = ex.Message;
                reObj.success = false;
            }
            return reObj;
        }

        /// <summary>
        /// 获取教师科目
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultObj<IdName>> TeacherCourseList()
        {
            ResultObj<IdName> reObj = new ResultObj<IdName>();
            try
            {
                reObj.success = true;
                reObj.dataList = (await dbHelper_TeacherCourse.FindAll(x => x.teacher_id != 0)).Select(x => new IdName { id = x.teacher_course_id.ToString(), name = x.teacher_name + ":" + x.course_name }).OrderBy(x=>x.name).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(typeof(PublicController), ex.ToString());
                reObj.msg = ex.Message;
                reObj.success = false;
            }
            return reObj;
        }


        /// <summary>
        /// 获取教师科目
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultObj<IdName>> TeacherWeekCourseList(DtoDo<int> inObj)
        {
            ResultObj<IdName> reObj = new ResultObj<IdName>();
            try
            {

                DapperHelper<CTeacherCourseEntity> dapperHelper=new DapperHelper<CTeacherCourseEntity>();
                var teacherCourse=await dapperHelper.Single(x=>x.teacher_course_id==inObj.Key);

                reObj.success = true;
                reObj.dataList = (await dbHelper_weekCourseV.FindAll(x => x.teacher_id == teacherCourse.teacher_id)).Select(x => new IdName { id = x.week_course_id.ToString(), name = $"周{x.order_num % 10}第{x.order_num / 10}节" }).OrderBy(x=>x.name).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(typeof(PublicController), ex.ToString());
                reObj.msg = ex.Message;
                reObj.success = false;
            }
            return reObj;
        }


        /// <summary>
        /// 获取科目列表
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultObj<IdName>> CourseList()
        {
            ResultObj<IdName> reObj = new ResultObj<IdName>();
            try
            {
                reObj.success = true;
                reObj.dataList = (await dbHelper_course.FindAll(x => x.course_id != 0)).Select(x => new IdName { id = x.course_id.ToString(), name = x.name }).OrderBy(x=>x.name).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(typeof(PublicController), ex.ToString());
                reObj.msg = ex.Message;
                reObj.success = false;
            }
            return reObj;
        }

    }
}